package azalea.mqtt.message.builder.impl;

import azalea.mqtt.message.utils.ByteUtils;
import azalea.mqtt.message.utils.LengthUtil;
import azalea.mqtt.message.utils.MsgUtils;

/**
 * 
|Name           |Value          |Direction of flow                   |Description
|Reserved       |0              |Forbidden                   |Reserved
|CONNECT        |1              |Client to Server                       |
|CONNACK        |2              |Server to Client                       |Connect acknowledgment
|PUBLISH        |3              |Client to Server or Server to Client   |Publish message
|PUBACK         |4              |Client to Server or Server to Client   |Publish acknowledgment
|PUBREC         |5              |Client to Server or Server to Client   |Publish received (assured delivery part 1)
|PUBREL         |6              |Client to Server or Server to Client   |Publish release (assured delivery part 2)
|PUBCOMP        |7              |Client to Server or Server to Client   |Publish complete (assured delivery part 3)
|SUBSCRIBE      |8              |Client to Server                       |Client subscribe request
|SUBACK         |9              |Server to Client                       |Subscribe acknowledgment
|UNSUBSCRIBE    |10             |Client to Server                       |Unsubscribe request
|UNSUBACK       |11             |Server to Client                       |Unsubscribe acknowledgment
|PINGREQ        |12             |Client to Server                       |PING request
|PINGRESP       |13             |Server to Client                       |PING response
|DISCONNECT     |14             |Client to Server                       |Client is disconnecting
|Reserved       |15             |Forbidden                              |Reserved
 * @author Administrator
 *
 */
public class ConnectionDataImpl {
	public static byte[] connect(byte protocolLevel,byte connectFlag,int keepalive,byte[] payload,int payLoadLen) {
		byte fixhead=0x10;
		int len=payLoadLen+10;
		byte[] byteLen=LengthUtil.calc(len);
		byte[] varhead= {(byte)0,(byte)4,'M','Q','T','T',protocolLevel,connectFlag,(byte)(keepalive>>8&0x0f),(byte)(keepalive&0x0f)};
		byte[] result=new byte[1+byteLen.length+len];
		result[0]=fixhead;
		ByteUtils.copy(byteLen, result, 0, 1, byteLen.length);
		ByteUtils.copy(varhead, result, 0, 1+byteLen.length, 10);
		ByteUtils.copy(payload, result, 0, 1+byteLen.length+10, payLoadLen);
		return result;
	}
	
	public static byte[] connack(boolean sessionPresent,byte response) {
		/**
		 * 
|Value  |Return Code Response                                   |Description
|0      |0x00 Connection Accepted                               |Connection accepted
|1      |0x01 Connection Refused, unacceptable protocol version |The Server does not support the level of the MQTT protocol requested by the Client
|2      |0x02 Connection Refused, identifier rejected           |The Client identifier is correct UTF-8 but not allowed by the Server
|3      |0x03 Connection Refused, Server unavailable            |The Network Connection has been made but the MQTT service is unavailable
|4      |0x04 Connection Refused, bad user name or password     |The data in the user name or password is malformed
|5      |0x05 Connection Refused, not authorized                |The Client is not authorized to connect
|6-255  |                                                       |Reserved for future use
		 */
		byte sessionPresentByte=sessionPresent?(byte)0b0000001:(byte)0b0000000;
		return new byte[]{(byte)0b00100000,(byte)0b0000010,sessionPresentByte,response};
	}
	public static byte[] disconnect() {
		//DISCONNECT包是客户端发给服务端的最后一个控制包。它表示客户端正在干净利索的断开连接。
		return new byte[]{(byte)0b11100000,(byte)0b0000000};
	}
}
